package com.cheng.common.utils;

import org.apache.ibatis.io.Resources;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.Reader;
import java.util.*;

/**
 * @author: ChengJW
 * @date: 2021/11/1 20:31
 * @version: 1.0
 * @description: 自定义异常编码
 */
@Component
public class CodeUtil {

    private final static Logger log = LoggerFactory.getLogger(CodeUtil.class);

    /** 存储自定义的异常编码和解释*/
    public static HashMap<String,String> CODE_MAP;

    /** 空指针*/
    public static String CJW03 = "CJW202111010003";

    /** 指定的类不存在*/
    public static String CJW04 = "CJW202111010004";

    /** 数组下标越界*/
    public static String CJW05 = "CJW202111010005";

    /** 字符串转换为数字异常*/
    public static String CJW06 = "CJW202111010006";

    /** 没有访问权限*/
    public static String CJW07 = "CJW202111010007";

    /** 算术异常*/
    public static String CJW08 = "CJW202111010008";

    /** 强制类型转换异常*/
    public static String CJW09 = "CJW202111010009";

    /** 数组负下标异常*/
    public static String CJW10 = "CJW202111010010";

    /** 文件已结束异常*/
    public static String CJW11 = "CJW202111010011";

    /** 文件未找到异常*/
    public static String CJW12 = "CJW202111010012";

    /** 操作数据库异常*/
    public static String CJW13 = "CJW202111010013";

    /** 输入输出异常*/
    public static String CJW14 = "CJW202111010014";

    /** 方法未找到异常*/
    public static String CJW15 = "CJW202111010015";

    /** 方法的参数错误*/
    public static String CJW16 = "CJW202111010016";

    /** shiro身份认证异常*/
    public static String CJW22 = "CJW202111010022";

    /** username or password is wrong*/
    public static String CJW23 = "CJW202111010023";

    /** BadSql异常*/
    public static String CJW25 = "CJW202111010025";

    /** 登录账户不存在*/
    public static String CJW31 = "CJW202111010031";

    /** 参数转换异常*/
    public static String CJW59 = "CJW202111010059";

    /** springMVC前后端方法参数类型不一致，并且框架默认无法转换*/
    public static String CJW60 = "CJW202111010060";

    /** 操作成功标志*/
    public static String SUCCESS_CODE = "00";

    /** 操作成功消息*/
    public static String SUCCESS_MSG = "Operation was successful";

    /** 操作失败标志*/
    public static String FAILURE_CODE = "01";

    /** 操作失败消息*/
    public static String FAILURE_MSG = "Operation failed";

    /** 出现异常标志*/
    public static String EXCEPTION_CODE = "02";

    /** 出现异常消息*/
    public static String EXCEPTION_MSG = "出现异常，请联系管理员！";

    /** 密码加密时用到的字符串*/
    public static String PASSWORD_SECURITY = "Qa1Wb2Ec3Rd4Te5Yf6Ug7Ih8OiPjAkSlDmFn9GoHpJqKrL0sZtXwCvVuBxNyMz_@!$";

    /** 密码加密盐值*/
    public static String SALT = "ChengJW1991";

    /** 异常信息长度为2000*/
    public static int EXCE_MSG_LENGTH = 2000;

    public static String DEFAULT_PROFILE_PHOTO_PATH = "/images/defaultphoto.jpg";

    /** 验证码字符串*/
    public static String CHECK_STRING ="QWERTYUIOPASDFGHJKLZXCVBNMqazwsxedcrfvtgbyhnujmikolp1234567890";

    /** 上传文件类型*/
    public static String FILE_CLASS_JPG = "jpg";

    public static String FILE_CLASS_PNG = "png";

    public static String FILE_CLASS_DOC = "doc";

    public static String FILE_CLASS_DOCX = "docx";

    public static String FILE_CLASS_XLS = "xls";

    public static String FILE_CLASS_XLSX = "xlsx";

    /** 日期格式*/
    public static  String DATE_STRING = "yyyy-MM-dd";

    public static String USER_CODE = "userCode";

    /** 系统版本*/
    public static String VERSION = "V1.0.0";

    /** 作者*/
    public static String AUTHOR = "ChengJW";

    /** 头像路径*/
    public static String PROFILE_PHOTO_PATH = "Profile_Photo_Path";

    /** 是否是开发模式标志*/
    public static String IS_OPEN_DEVELOPMENT_CD = "Is_Open_Development_Cd";

    /** 开发模式*/
    public static String DEVELOPMENT_MODEAL = "1";

    /** 图像存放路径*/
    public static String IMAGES_PATH = "/images/";

    /** word 存放路径*/
    public static String DOCS_PATH = "/docs/";

    /** excel 存放路径*/
    public static String EXCELS_PATH = "/xls/";

    public static String USER_NM = "userNm";

    /** 分隔符*/
    public static String SEPARATOR = "\\";

    /** 当前日期*/
    public static String NOW_DATE = "nowDate";

    public static String SRCH_REQ_FIR = "srchReqFir";

    public static String QRY_REQ_FIR= "qryReqFir";

    public static String SRCH_REQ_SECO = "srchReqSeco";

    public static String QRY_REQ_SECO= "qryReqSeco";

    /** 表示未做迁移或者迁移完成*/
    public static String MOVE_STD_0 = "0";

    /** 表示档案迁出但是未迁入状态*/
    public static String MOVE_STD_1 = "1";

    /** 表示未借阅或者已归还*/
    public static String READ_STD_0 = "0";

    /** 表示出借未归还*/
    public static String READ_STD_1 = "1";

    /** 表示整户借阅*/
    public static String BROW_TYPE_STD_0 = "0";

    /** 表示单卷借阅*/
    public static String BROW_TYPE_STD_1 = "1";

    // 统计类型
    public static String QI_YE = "qiYe";

    public static String DANG_AN = "dangAn";

    public static String SHOU_LI = "shouLi";

    public static String QIAN_YI = "qianYi";

    public static String JIE_YUE = "jieYue";

    /** 统计时条件为 全部分局代码*/
    public static String STATISTIC_TYPE = "350000";

    public static String STATISTIC_CODE = "statisticCode";


    /**
    * @author: ChengJW
    * @params: []
    * @return: void
    * @date: 2021/11/2
    * @description: 加载自定义异常编码
    */
    @PostConstruct
    public void getHashMap(){
        Reader reader = null;
        HashMap<String,String> hashMap = new HashMap<>();
        try {
            reader = Resources.getResourceAsReader("application/code.properties");
            Properties properties = new Properties();
            properties.load(reader);
            if (properties.size() > 0){
                Set<Object> set = properties.keySet();
                for (Object obj : set){
                    hashMap.put(String.valueOf(obj),String.valueOf(properties.get(obj)));
                }
            }

        } catch (IOException e) {
            log.info("加载自定义异常编码失败:application/code.properties");
            e.printStackTrace();
        }finally {
            if (null != reader){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        CODE_MAP = hashMap;
    }
}
